package com.ukao.pad.ui.collectMoney;


import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.base.MvpFragment;
import com.ukao.pad.bean.BalanceBean;
import com.ukao.pad.bean.Coupons;
import com.ukao.pad.bean.PayInfoBean;
import com.ukao.pad.dialog.CouponsDialogFragment;
import com.ukao.pad.eventbus.PayAmountEvent;
import com.ukao.pad.eventbus.PayEvent;
import com.ukao.pad.presenter.GatheringPesenter;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.DensityUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.view.GatheringView;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

import static com.ukao.pad.R.id.should_pay_layout;


/**
 * 收款碎片
 */
public class GatheringFragment extends MvpFragment<GatheringPesenter> implements GatheringView {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.back_layout)
    RelativeLayout backLayout;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.orderPrice)
    TextView orderPrice;
    @BindView(R.id.carriage)
    TextView carriage;
    @BindView(R.id.modifyPrice)
    TextView modifyPrice;
    @BindView(R.id.modifyPrice_layout)
    LinearLayout modifyPriceLayout;
    @BindView(R.id.paymentPrice)
    TextView paymentPrice;
    @BindView(R.id.txt_coupon)
    TextView txtCoupon;
    @BindView(R.id.couponAmt)
    TextView tvCouponAmt;
    @BindView(R.id.usable_balance)
    TextView usableBalance;
    @BindView(R.id.balance)
    TextView vipCardBalance;
    @BindView(R.id.enabled_imageview)
    ImageView enabledImageview;
    //可用余额支付单选框
    @BindView(R.id.enabled_checkBox)
    AppCompatCheckBox enabledCheckBox;
    //会员卡余额布局
    @BindView(R.id.pay_type_one)
    LinearLayout vipCardBalanceLayout;
    @BindView(R.id.sure_gathering_btn)
    TextView sureGatheringBtn;
    @BindView(R.id.should_Price)
    TextView shouldPrice;
    @BindView(should_pay_layout)
    LinearLayout shouldPayLayout;
    @BindView(R.id.gathering_layout)
    RelativeLayout gatheringLayout;
    @BindView(R.id.order_detail_layout)
    LinearLayout orderDetailLayout;
    @BindView(R.id.order_detail_tab1)
    RelativeLayout orderDetailTab1;
    @BindView(R.id.order_detail_tab2)
    RelativeLayout orderDetailTab2;
    @BindView(R.id.order_detail_tab3)
    RelativeLayout orderDetailTab3;
    @BindView(R.id.order_detail_tab4)
    RelativeLayout orderDetailTab4;
    @BindView(R.id.frame_layout)
    LinearLayout frameLayout;
    @BindView(R.id.back_btn)
    TextView backBtn;
    @BindView(R.id.sure_gather_txt)
    TextView sureGatherTxt;
    @BindView(R.id.pay_layout)
    LinearLayout payLayout;

    //优付卡支付单选框
    @BindView(R.id.preferential_radio_button)
    AppCompatCheckBox preferentialRadioButton;
    //余额支付单选框
    @BindView(R.id.balance_radio_button)
    AppCompatCheckBox balanceRadioButton;
    //余额支付和优付卡支付布局
    @BindView(R.id.pay_type_two)
    LinearLayout payTypeTwo;
    //余额支付文本
    @BindView(R.id.usable_balance_msg)
    TextView tvBalancePay;
    //可用余额文本
    @BindView(R.id.usable_balance_two)
    TextView tvUsableBalance;
    //余额支付布局
    @BindView(R.id.balance_radio_layout)
    LinearLayout balanceRadioLayout;

    @BindView(R.id.preferential_msg)
    TextView preferentialMsg;
    @BindView(R.id.preferential_balance)
    TextView preferentialBalance;
    @BindView(R.id.preferential_payRatio)
    TextView preferentialPayRatio;
    @BindView(R.id.preferential_should_pice)
    TextView preferentialShouldPice;
    @BindView(R.id.preferential_radio_layout)
    LinearLayout preferentialRadioLayout;
    @BindView(R.id.cloth_layout)
    LinearLayout clothLayout;
    @BindView(R.id.balance_should_pice)
    TextView balanceShouldPice;
    @BindView(R.id.couponAmt_layout)  //优惠卷布局
    LinearLayout couponAmtLayout;


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private PayInfoBean mPayInfo;
    private SupportFragment[] mFragments = new SupportFragment[4];
    private int totalBalance;
    private  boolean []  payShowArray=new  boolean[4];
    private int lastPosition;
    private RelativeLayout[] payTabArray;
    private int firstPosition;
    private PayInfoBean.UPayCardBean   uPayCard;

    public static GatheringFragment newInstance(PayInfoBean mPayInfoBean, String param2) {
        GatheringFragment fragment = new GatheringFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mPayInfoBean);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPayInfo = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected GatheringPesenter createPresenter() {
        return new GatheringPesenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gathering, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    /**
     * 显示加载子Fragment
     */
    private void showLoadChildFragment(){
        payShowArray = new boolean[]{mPayInfo.isIsEnableAli(), mPayInfo.isIsEnableWx(), mPayInfo.isIsEnableCash(), mPayInfo.isEnableOther()};
        payTabArray=new  RelativeLayout[]{orderDetailTab1,orderDetailTab2,orderDetailTab3,orderDetailTab4};
        boolean first=false;
        for (int i = 0; i < payShowArray.length; i++) {
            if (payShowArray[i]){
                payTabArray[i].setVisibility(View.VISIBLE);
                lastPosition=i;
            }
            if (!first&&payShowArray[i]){
                first=true;
                firstPosition=i;
            }
        }
        setBackGround(firstPosition);
        SupportFragment firstFragment = findFragment(AlipayWeChatFragment.class);
        if (firstFragment == null) {
            mFragments[0] = AlipayWeChatFragment.newInstance(mPayInfo,false);
            mFragments[1] = AlipayWeChatFragment.newInstance(mPayInfo,true);
            mFragments[2] = CashFragment.newInstance(mPayInfo);
            mFragments[3] = UnionpayFragment.newInstance(mPayInfo);
            loadMultipleRootFragment(R.id.frame_layout,firstPosition,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3]);
        } else {
            mFragments[0] = firstFragment;
            mFragments[1] = findFragment(AlipayWeChatFragment.class);
            mFragments[2] = findFragment(CashFragment.class);
            mFragments[3] = findFragment(UnionpayFragment.class);
        }
    }

    @Override
    protected void initView() {
        mvpPresenter.userExtbalance(mPayInfo.getUserId());
    }


    @Override
    protected boolean useEventBus() {
        return true;
    }


    @Subscribe
    public void onEvent(PayEvent event) {
        switch (event.getMessage()) {
            case PAY_SUCCEED:
                dismissProgressDialog();
                setFragmentResult(RESULT_OK, null);
                finish();
                break;
            case   coupon :  //优惠卷
                //设置优惠卷的样式
                this.setCouponsStyle(mPayInfo.getCpnRelId(),mPayInfo.getCouponAmt());
                this.setVipCardBalanceAndPreferentialPayEnabled();  //设置余额支付和优付卡是否能点击
                if (preferentialRadioButton.isChecked()) {
                    this.setUpayStyles();
                } else {
                    this.reckonShouldPrice();  //计算应收款
                }
                break;

        }
    }

    /**
     * 设置优惠卷的样式
     * couponId 优惠卷id
     *couponAmt 优惠卷金额
     */
    private  void  setCouponsStyle(String couponId,int couponAmt){
        if (!TextUtils.isEmpty(couponId) && couponAmt!=0){
            tvCouponAmt.setText("-" + CheckUtils.isEmptyNumber(couponAmt));/*优惠券*/
            tvCouponAmt.setTextColor(getResources().getColor(R.color.warm_red));
        }else {
            tvCouponAmt.setTextColor(getResources().getColor(R.color.txt_gray));
            tvCouponAmt.setText("无可用");
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        enabledCheckBox.setOnCheckedChangeListener(mOnCheckedChange);
        balanceRadioButton.setClickable(false);
        preferentialRadioButton.setClickable(false);

    }
    /**
     * 设置使用优付卡样式
     */
    private void setUpayStyles() {
        int shouldPrice;
        if(preferentialRadioButton.isChecked()){
            mPayInfo.setUseUPay(true);   //使用了优付卡支付
            mPayInfo.setPayBalance(0);
            if(!couponAmtLayout.isEnabled()){  //不能与优惠卷同时使用时
                int useBalance;
                int mRatioPice = (mPayInfo.getPayablePrice() * uPayCard.getPayRatio()) / 100;  /*得到可支付比例金额*/
                int uPayPayPice = mRatioPice - uPayCard.getBalance();/*还应收款金额*/
                if (uPayPayPice > 0) {  /*余额不足*/
                    shouldPrice = mPayInfo.getPayablePrice() - uPayCard.getBalance();
                    mPayInfo.setShouldPrice(shouldPrice);
                    useBalance = uPayCard.getBalance();
                } else { /*余额充足*/
                    useBalance = mRatioPice;
                    if (mPayInfo.getPayablePrice() - mRatioPice > 0) {/*应收款*/
                        shouldPrice = mPayInfo.getPayablePrice() - mRatioPice;
                    } else {
                        shouldPrice = 0;
                    }
                }
                preferentialShouldPice.setText("抵扣" + CheckUtils.isEmptyNumber(useBalance) + "元");
                mPayInfo.setShouldPrice(shouldPrice);
                setShouldPriceStyle(shouldPrice);
            }else { // 计算能与优惠卷同时使用价格
                int couponAmt1 = mPayInfo.getCouponAmt();
                int remainPrice = mPayInfo.getPayablePrice() - couponAmt1;  /*优惠后的金额*/
                int useBalance;
                int mRatioPice = (remainPrice * uPayCard.getPayRatio()) / 100;  /*得到可支付比例金额*/
                int uPayPayPice = mRatioPice - uPayCard.getBalance();/*还应收款金额*/
                if (uPayPayPice > 0) {  /*余额不足*/
                    shouldPrice = remainPrice - uPayCard.getBalance();
                    mPayInfo.setShouldPrice(shouldPrice);
                    useBalance = uPayCard.getBalance();
                } else { /*余额充足*/
                    useBalance = mRatioPice;
                    if (remainPrice - mRatioPice > 0) {/*应收款*/
                        shouldPrice = remainPrice - mRatioPice;
                    } else {
                        shouldPrice = 0;
                    }
                }
                preferentialShouldPice.setText("抵扣" + CheckUtils.isEmptyNumber(useBalance) + "元");
                mPayInfo.setShouldPrice(shouldPrice);
                setShouldPriceStyle(shouldPrice);
            }
        }else {
            mPayInfo.setPayBalance(0);
            mPayInfo.setUseUPay(false);  //没有使用优付卡支付
            shouldPrice = mPayInfo.getPayablePrice() - mPayInfo.getCouponAmt();
            mPayInfo.setShouldPrice(shouldPrice);
            preferentialShouldPice.setText("");
            setShouldPriceStyle(shouldPrice);
        }

    }

    /**
     * 计算余额支付和会员卡余额支付应收款
     * 支付金额    优惠券  余额
     */
    private void reckonShouldPrice() {
        if (enabledCheckBox.isChecked() || balanceRadioButton.isChecked()) {
            int remainPrice = mPayInfo.getPayablePrice() - mPayInfo.getCouponAmt();/*扣去优惠后的金额*/
            int useBalance;
            if (totalBalance - remainPrice < 0) { /*总余额 扣去优惠后的金额*/
                useBalance = totalBalance;   //
            } else {  /*完全够余额付款*/
                useBalance = remainPrice;
            }
            mPayInfo.setPayBalance(useBalance);//获得 要支付的余额
            int shouldPayPrice = mPayInfo.getPayablePrice() - useBalance - mPayInfo.getCouponAmt();
            mPayInfo.setShouldPrice(shouldPayPrice);  //设置总应收款
            setShouldPriceStyle(mPayInfo.getShouldPrice());
            if (vipCardBalanceLayout.getVisibility() == View.VISIBLE) {  /*会员卡余额支付*/
                vipCardBalance.setText(useBalance > 0 ? "-" + CheckUtils.isEmptyNumber(useBalance) : "");/*余额*/
            } else {   //余额支付
                balanceShouldPice.setText(useBalance > 0 ? "抵扣" + CheckUtils.isEmptyNumber(useBalance) + "元" : "");
            }
        } else {
            balanceShouldPice.setText("");//设置抵扣的余额为""
            vipCardBalance.setText("");/*会员卡余额*/
            mPayInfo.setPayBalance(0);
            int remainPrice = mPayInfo.getPayablePrice() - mPayInfo.getCouponAmt();/*应收款*/
            mPayInfo.setShouldPrice(remainPrice);
            setShouldPriceStyle(remainPrice >= 0 ? remainPrice : 0);
        }
    }

    /**
     * @param isNotnough 是否第三方支付显示
     */
    private void setPayVisibility(boolean isNotnough) {
        shouldPayLayout.setVisibility(isNotnough ? View.GONE : View.VISIBLE);
        payLayout.setVisibility(isNotnough ? View.GONE : View.VISIBLE);
        sureGatheringBtn.setVisibility(isNotnough ? View.VISIBLE : View.GONE);
        sureGatherTxt.setVisibility(isNotnough ? View.VISIBLE : View.GONE);
    }


    /**
     * 设置 *还应收款样式* 金额
     * @param price
     */
    private void setShouldPriceStyle(int price) {
        if (price>0){
            gatheringLayout.setBackgroundResource(R.drawable.shape_button_red_select);  //红色
            setPayVisibility(false);
        }else{
            gatheringLayout.setBackgroundResource(R.drawable.blue_bt_selector);  //蓝色
            setPayVisibility(true);
        }
        String paidPrice = "￥"+CheckUtils.isEmptyNumber(price);
        int index = paidPrice.indexOf(".");
        if (index != -1) {
            Spannable span = new SpannableString(paidPrice);
            span.setSpan(new AbsoluteSizeSpan(CheckUtils.isMIUIDevices()?80:120),1,index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            shouldPrice.setText(span);
                UnionpayFragment unionpayFragment= (UnionpayFragment) mFragments[3];
                unionpayFragment.setGatheringMoney();
        }
    }

    private CompoundButton.OnCheckedChangeListener mOnCheckedChange = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    switch (buttonView.getId()){
                        case R.id.enabled_checkBox:  //会员卡余额被选中、
                            reckonShouldPrice();
                            break;
                    }
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 用户余额
     *
     * @param mBalanceBean
     */
    @Override
    public void useBalanceData(BalanceBean mBalanceBean) {
        //这个 方法必须走前面
        this.showLoadChildFragment();
        Integer intModifyPrice = mPayInfo.getModifyPrice(); /*修改金额*/
        if (CheckUtils.isNull(intModifyPrice)) {
            modifyPriceLayout.setVisibility(View.GONE);
        } else {
            modifyPriceLayout.setVisibility(View.VISIBLE);
            modifyPrice.setText("¥ " + CheckUtils.isEmptyNumber(mPayInfo.getModifyPrice()));
        }
        orderPrice.setText("￥" + CheckUtils.isEmptyNumber(mPayInfo.getOrderPrice()));/*衣物总额*/
        carriage.setText("￥" + CheckUtils.isEmptyNumber(mPayInfo.getCarriage()));/*取送费*/
        paymentPrice.setText("￥" + CheckUtils.isEmptyNumber(mPayInfo.getPayablePrice()));/*应收款*/
        totalBalance = mBalanceBean.getBalance();
        //获得优付卡所有数据
       uPayCard = mPayInfo.getUPayCard();
        //设置优惠卷的样式
        this.setCouponsStyle(mPayInfo.getCpnRelId(),mPayInfo.getCouponAmt());
        if (!CheckUtils.isNull(uPayCard)) {   /*是否存在优付卡支付*/
           /*会员卡余额布局隐藏*/
            vipCardBalanceLayout.setVisibility(View.GONE);
            //余额支付和优付卡支付显示
            payTypeTwo.setVisibility(View.VISIBLE);
              /*判断是否能使用余额支付*/
            if (totalBalance <= 0) {
                //小于0  余额支付不可用用
                tvBalancePay.setTextColor( getResources().getColor(R.color.txt_gray));
                tvUsableBalance.setTextColor( getResources().getColor(R.color.txt_gray));
                balanceRadioButton.setButtonDrawable(R.drawable.pay_check_endle);
                balanceRadioLayout.setEnabled(false);
            } else {
                //余额支付可以用 设置为选中
                balanceRadioButton.setChecked(true);
            }
            //设置余额支付的可用余额
            tvUsableBalance.setText("可用余额 " + CheckUtils.isEmptyNumber(totalBalance) + "元");
            /*判断是否能使用优付卡支付*/
            if (uPayCard.getBalance() <= 0 || uPayCard.getPayRatio() == 0|| !uPayCard.getIsEnable()) {
                preferentialMsg.setTextColor(getResources().getColor(R.color.txt_gray));
                preferentialBalance.setTextColor(getResources().getColor(R.color.txt_gray));
                preferentialPayRatio.setTextColor(getResources().getColor(R.color.txt_gray));
                preferentialRadioButton.setButtonDrawable(R.drawable.pay_check_endle);
                preferentialRadioLayout.setEnabled(false);
            }
            this.setVipCardBalanceAndPreferentialPayEnabled();
            preferentialBalance.setText("余额 " + CheckUtils.isEmptyNumber(uPayCard.getBalance()) + "元");
            preferentialPayRatio.setText("支付比例 " + uPayCard.getPayRatio() + "%");
            reckonShouldPrice();
        } else {
            if(totalBalance<=0){  //不可用
                enabledImageview.setVisibility(View.VISIBLE);
                enabledCheckBox.setVisibility(View.GONE);
                vipCardBalanceLayout.setEnabled(false);  //静止使用
            }
            this.setVipCardBalanceAndPreferentialPayEnabled();
            //设置会员卡的可用余额
            usableBalance.setText("可用余额" + CheckUtils.isEmptyNumber(totalBalance));
            ViewGroup.LayoutParams mParams = clothLayout.getLayoutParams();
            mParams.height = DensityUtils.dip2px(330);
            clothLayout.setLayoutParams(mParams);
            reckonShouldPrice();
        }
    }
    /**
     * 设置会员卡余额和优付卡是否能用
     */
    private  void  setVipCardBalanceAndPreferentialPayEnabled(){
        //会员卡余额布局显示
      if(vipCardBalanceLayout.getVisibility()==View.VISIBLE){
          if(vipCardBalanceLayout.isEnabled()){
              if(mPayInfo.getCouponAmt()>= mPayInfo.getPayablePrice()){   //如果优惠卷金额大于等于 应收款金额 就不能用会员卡余额支付
                  enabledImageview.setVisibility(View.VISIBLE);
                  enabledCheckBox.setVisibility(View.GONE);
                  enabledCheckBox.setChecked(false);
              }else if(mPayInfo.getCpnUseBalance()==1){ // 等于1 允许使用会员卡余额金额
                  enabledImageview.setVisibility(View.GONE);
                  enabledCheckBox.setVisibility(View.VISIBLE);
              }else if(mPayInfo.getCpnUseBalance()==0){     //不允许使用会员卡余额金额
                  enabledImageview.setVisibility(View.VISIBLE);
                  enabledCheckBox.setVisibility(View.GONE);
                  enabledCheckBox.setChecked(false);
              }
          }
      }else if(payTypeTwo.getVisibility()==View.VISIBLE) {  //余额支付和优付卡布局显示
          if (mPayInfo.getCouponAmt() >= mPayInfo.getPayablePrice()) {   //如果优惠卷金额大于等于
              if (balanceRadioLayout.isEnabled()) {
                  //余额支付能用的才设置
                  tvBalancePay.setTextColor(getResources().getColor(R.color.txt_gray));
                  tvUsableBalance.setTextColor(getResources().getColor(R.color.txt_gray));
                  balanceRadioButton.setButtonDrawable(R.drawable.pay_check_endle);
                  balanceShouldPice.setText("");
                  balanceRadioLayout.setClickable(false);
                  balanceRadioButton.setChecked(false);
              }
              if (preferentialRadioLayout.isEnabled()) {
                  //优付卡支付能用才设置
                  preferentialMsg.setTextColor(getResources().getColor(R.color.txt_gray));
                  preferentialBalance.setTextColor(getResources().getColor(R.color.txt_gray));
                  preferentialPayRatio.setTextColor(getResources().getColor(R.color.txt_gray));
                  preferentialRadioButton.setButtonDrawable(R.drawable.pay_check_endle);
                  preferentialShouldPice.setText("");
                  preferentialRadioLayout.setClickable(false);
                  preferentialRadioButton.setChecked(false);
              }
          } else if (mPayInfo.getCpnUseBalance() == 1) { // 等于1 允许使用会员卡余额金额
              //余额支付能用就设置
              if (balanceRadioLayout.isEnabled()) {
                  if (!balanceRadioLayout.isClickable()) {
                      balanceRadioButton.setButtonDrawable(R.drawable.pay_checkbox_style);
                      tvBalancePay.setTextColor(getResources().getColor(R.color.black));
                      tvUsableBalance.setTextColor(getResources().getColor(R.color.black));
                      balanceRadioLayout.setClickable(true);  //设置可用点击
                  }
              }
              if (preferentialRadioLayout.isEnabled()) { //能用就设置
                  if (!preferentialRadioLayout.isClickable()) {
                      preferentialMsg.setTextColor(getResources().getColor(R.color.black));
                      preferentialBalance.setTextColor(getResources().getColor(R.color.black));
                      preferentialPayRatio.setTextColor(getResources().getColor(R.color.black));
                      preferentialRadioButton.setButtonDrawable(R.drawable.pay_checkbox_style);
                      preferentialRadioLayout.setClickable(true); //设置可用点击
                  }
              }
          } else if (mPayInfo.getCpnUseBalance() == 0) {     //不允许使用会员卡余额金额
              if (balanceRadioLayout.isEnabled()) {
                  if (balanceRadioLayout.isClickable()) {
                      balanceShouldPice.setText("");
                      tvBalancePay.setTextColor(getResources().getColor(R.color.txt_gray));
                      tvUsableBalance.setTextColor(getResources().getColor(R.color.txt_gray));
                      balanceRadioButton.setButtonDrawable(R.drawable.pay_check_endle);
                      balanceRadioLayout.setClickable(false);
                      balanceRadioButton.setChecked(false);
                  }
              }
              if (preferentialRadioLayout.isEnabled()) { //能用就设置
                  if (!preferentialRadioLayout.isClickable()) {
                      preferentialMsg.setTextColor(getResources().getColor(R.color.black));
                      preferentialBalance.setTextColor(getResources().getColor(R.color.black));
                      preferentialPayRatio.setTextColor(getResources().getColor(R.color.black));
                      preferentialRadioButton.setButtonDrawable(R.drawable.pay_checkbox_style);
                      preferentialRadioLayout.setClickable(true); //设置可用点击
                  }
              }
          }
      }
    }

    @Override
    public void loadFail(String msg) {
        T.show(msg);
    }

    @Override
    public void loadCouponList(List<Coupons> mCouponData) {
        CouponsDialogFragment coupons = new CouponsDialogFragment();
        Bundle mBundle = new Bundle();
        mBundle.putBoolean("isPayEnter", true);
        mBundle.putParcelableArrayList(ARG_PARAM1, (ArrayList<? extends Parcelable>) mCouponData);
        mBundle.putParcelable(ARG_PARAM2, mPayInfo);
        coupons.setArguments(mBundle);
        coupons.show(getChildFragmentManager(), CouponsDialogFragment.class.getName());
    }

    @Override
    public void userCouponSucceed(Coupons mCoupons) {
        if (mPayInfo !=null){
            mPayInfo.setCouponAmt(mCoupons.getAmount());
            tvCouponAmt.setText("-" + CheckUtils.isEmptyNumber(mCoupons.getAmount()));/*优惠券*/
            reckonShouldPrice();
        }
    }


    @OnClick({R.id.back_btn, R.id.gathering_layout,
            R.id.order_detail_tab1, R.id.order_detail_tab2,
            R.id.order_detail_tab3, R.id.order_detail_tab4,
            R.id.couponAmt_layout, R.id.balance_radio_layout,
            R.id.preferential_radio_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case  R.id.couponAmt_layout:  //打开优惠卷窗口
                mvpPresenter.couponList(mPayInfo.getOrderId());
                break;
            case R.id.back_btn:
                finish();
                break;
            case R.id.gathering_layout:
                if(mPayInfo.getShouldPrice()==0){
                    if (mPayInfo.isUseUPay()) {  /*优付卡余额充足时*/
                            mvpPresenter.confirmPay(mPayInfo.getOrderId(),mPayInfo.getCpnRelId());
                    } else {  /*完全余额时*/
                            mvpPresenter.balancePay(mPayInfo.getOrderId(), mPayInfo.getCpnRelId());
                    }
                }
                break;
            case R.id.order_detail_tab1:
                setBackGround(0);
                showHideFragment(mFragments[0]);
                break;
            case R.id.order_detail_tab2:
                setBackGround(1);
                showHideFragment(mFragments[1]);
                break;
            case R.id.order_detail_tab3:
                setBackGround(2);
                showHideFragment(mFragments[2]);
                break;
            case R.id.order_detail_tab4:
                setBackGround(3);
                showHideFragment(mFragments[3]);
//                UnionpayFragment unionpayFragment= (UnionpayFragment) mFragments[3];
//                unionpayFragment.get
//                unionpayFragment.setGatheringMoney();
//
                break;
            case R.id.balance_radio_layout:  //点击了余额支付布局
                if(balanceRadioButton.isChecked()){
                    balanceRadioButton.setChecked(false);
                }else {
                    balanceRadioButton.setChecked(true);
                    if(preferentialRadioButton.isChecked()){
                        //如果优惠卡选中了 设置为不使用优付卡
                        mPayInfo.setUseUPay(false);
                        preferentialRadioButton.setChecked(false);
                        preferentialShouldPice.setText("");
                    }
                }
                this.reckonShouldPrice();
                break;
            case R.id.preferential_radio_layout: //点击了优付卡支付布局
                 if(preferentialRadioButton.isChecked()){
                     preferentialRadioButton.setChecked(false);
                     if(!couponAmtLayout.isEnabled()){
                         couponAmtLayout.setEnabled(true);  //设置能用
                         this.setCouponsStyle(mPayInfo.getCpnRelId(),mPayInfo.getCouponAmt());
                     }
                 }else {
                     preferentialRadioButton.setChecked(true);
                     if(!uPayCard.getUseCoupon()){
                         //设置与优惠卷不能同时使用
                         tvCouponAmt.setTextColor(getResources().getColor(R.color.txt_gray));
                         couponAmtLayout.setEnabled(false);
                         tvCouponAmt.setText("不能与优付卡同时使用");
                     }
                     if(balanceRadioButton.isChecked()){
                         balanceRadioButton.setChecked(false);
                         balanceShouldPice.setText("");
                     }
                 }
                setUpayStyles();
                break;
        }
    }

    public void setBackGround(int pos) {
        if (pos == lastPosition) {
            payTabArray[pos].setBackgroundColor(resources.getColor(R.color.white));

        } else {
            payTabArray[pos].setBackgroundResource(R.drawable.gathering_tab_checked);

        }

        for (int i = 0; i < payTabArray.length; i++) {
            if (pos != i) {
                if (i == lastPosition) {
                    payTabArray[i].setBackgroundResource(R.drawable.gathering_right_tab);
                } else {
                    payTabArray[i].setBackgroundResource(R.drawable.gathering_tab);
                }
            }
        }
    }
}
